package com.cs456.TeamBobcat.views;

import java.util.Iterator;
import java.util.Vector;

import com.cs456.TeamBobcat.R;

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class StaticMapLayout extends FrameLayout {
	private static String TAG = "StaticMapLayout";

	ImageView currentLocation, map;
	PointF currentGPS = null;
	Vector<PointF> pastGPS = new Vector<PointF>();

	PointF topLeftGPS, bottomRightGPS;

	public StaticMapLayout(Context context) {
		super(context);
		Log.d(TAG, "StaticMapLayout(Context context)");
	}

	public StaticMapLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.d(TAG, "StaticMapLayout(Context context, AttributeSet attrs)");
	}

	public StaticMapLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.d(TAG, "StaticMapLayout(Context context, AttributeSet attrs, int defStyle)");
	}


	public void setMap(int mapID, float tlLat, float tlLong, float brLat, float brLong){
		Log.d(TAG, "setMap(int mapID, float tlLat, float tlLong, float brLat, float brLong)");

		removeAllViewsInLayout();
		map = new ImageView(getContext());
		map.setImageResource(mapID);
		map.setScaleType(ScaleType.FIT_XY);
		addView(map);

		topLeftGPS = new PointF(tlLong, tlLat);
		bottomRightGPS = new PointF(brLong, brLat);

	}

	public void setCurrentLocation(float lat, float lon){
		Log.d(TAG, "setCurrentLocation(float x, float y)");

		validateGPS(lat, lon);
		currentGPS = new PointF(lon, lat);

	}

	public void addPastLocation(float lat, float lon){
		Log.d(TAG, "addPastLocation(float x, float y)");

		validateGPS(lat, lon);
		pastGPS.add(new PointF(lon,lat));
	}

	private void validateGPS(float lat, float lon){
		Log.d(TAG, "validateGPS(float x, float y)");
		if(lon < topLeftGPS.x || lon > bottomRightGPS.x || lat < bottomRightGPS.y || lat > topLeftGPS.y)
			throw new RuntimeException("Invalid GPS Cooridinates");
	}

	protected void onLayout (boolean changed, int left, int top, int right, int bottom){
		Log.d(TAG, "onLayout (boolean changed, int left, int top, int right, int bottom){");

		super.onLayout(changed, left, top, right, bottom);
		Log.d(TAG, "Height: "+getHeight());
		Log.d(TAG, "Width: "+getWidth());
		Log.d(TAG, "Map Height: "+map.getHeight());
		Log.d(TAG, "Map Width: "+map.getWidth());

		int MAP_WIDTH = map.getWidth();
		int MAP_HEIGHT = map.getHeight();

		if(currentGPS != null){
			currentLocation = new ImageView(getContext());
			currentLocation.setImageResource(R.drawable.current_point);
			currentLocation.setScaleType(ScaleType.MATRIX);
			currentLocation.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

			Matrix mat = new Matrix();
			mat.postScale(.2f, .2f);


			float xscale = (currentGPS.x-topLeftGPS.x)/(bottomRightGPS.x - topLeftGPS.x)*MAP_WIDTH - 7;
			float yscale = (topLeftGPS.y - currentGPS.y)/(topLeftGPS.y - bottomRightGPS.y)*MAP_HEIGHT - 7;

			Log.d(TAG, "Cur X: "+xscale);
			Log.d(TAG, "Cur Y: "+yscale);

			mat.postTranslate(xscale, yscale);
			currentLocation.setImageMatrix(mat);
			addView(currentLocation);
			Log.d(TAG, "Point Height: "+currentLocation.getHeight());
			Log.d(TAG, "Point Width: "+currentLocation.getWidth());
		}

		Iterator<PointF> it = pastGPS.iterator();
		while(it.hasNext()){
			PointF point = it.next();
			ImageView past = new ImageView(getContext());
			past.setImageResource(R.drawable.past_point);
			past.setScaleType(ScaleType.MATRIX);

			Matrix mat = new Matrix();
			mat.postScale(.2f, .2f);


			float xscale = (point.x-topLeftGPS.x)/(bottomRightGPS.x - topLeftGPS.x)*MAP_WIDTH - 7;
			float yscale = (topLeftGPS.y - point.y)/(topLeftGPS.y - bottomRightGPS.y)*MAP_HEIGHT - 7;
			Log.d(TAG, "Past X: "+xscale);
			Log.d(TAG, "Past Y: "+yscale);

			mat.postTranslate(xscale, yscale);
			past.setImageMatrix(mat);
			past.bringToFront();

			addView(past);
		}
		

	}


}
